//
//  ViewController+Shortcuts.m
//  SiriShortcuts
//
//  Created by orvibo on 2019/1/2.
//  Copyright © 2019 orvibo. All rights reserved.
//

#import "ViewController+Shortcuts.h"

@implementation ViewController (Shortcuts)

-(void)controlScene:(HMScene *)scene
{
    [HMSceneAPI triggleSceneWithScene:scene completion:^(KReturnValue value, id object) {
        if (value == KReturnValueSuccess) {
            NSLog(@"情景执行成功");
        }else{
            NSLog(@"情景执行失败");
        }
    }];
}

-(void)handleSiriShortcut:(HMScene *)scene API_AVAILABLE(ios(12.0)){
    
    //if (@available(iOS 12.0, *)) {
    [[INVoiceShortcutCenter sharedCenter] getAllVoiceShortcutsWithCompletion:^(NSArray<INVoiceShortcut *> * _Nullable voiceShortcuts, NSError * _Nullable error) {
        BOOL isEdit = NO;
        INVoiceShortcut *shortcut = nil;
        for (INVoiceShortcut *voice in voiceShortcuts) {
            //NSLog(@"====>>获取到的VoiceShortcuts :%@",voice.shortcut.userActivity.activityType);
            NSLog(@"====>>获取到的VoiceShortcuts :%@",NSStringFromClass([voice.shortcut.intent class]));
            //if ([voice.shortcut.userActivity.activityType isEqualToString:@"com.xxx.xxx.orderquilkly"]) {
            if ([voice.shortcut.intent isKindOfClass:[HMSceneIntent class]]) {
                
                HMSceneIntent *intent = (HMSceneIntent *)voice.shortcut.intent;
                if([intent.sceneNo isEqualToString:scene.sceneNo]){
                    isEdit = YES;
                    shortcut = voice;
                    NSLog(@"===>>>快捷下单shortcut已经添加到Siri");
                    break;
                }
            }
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            if (isEdit) {
                [self editSiriShortcut:shortcut];
            } else {
                [self addSiriShortcut:scene];
            }
        });
    }];
    //}
}

-(void)donateInteration:(INVoiceShortcut *)voiceShortcut API_AVAILABLE(ios(12.0)){
    //if (@available(iOS 12.0, *)) {
    
    INInteraction *interaction = [[INInteraction alloc] initWithIntent:voiceShortcut.shortcut.intent response:nil];
    [interaction donateInteractionWithCompletion:^(NSError * _Nullable error) {
        NSLog(@"%@",error);
    }];
    //    } else {
    //        // Fallback on earlier versions
    //    }
}
-(void)addSiriShortcut:(HMScene *)scene API_AVAILABLE(ios(12.0)){
    
    HMSceneIntent *intent = [[HMSceneIntent alloc]init];
    intent.sceneNo = scene.sceneNo;
    intent.sceneId = @(scene.sceneId);
    intent.sceneName = scene.sceneName;
    intent.uidArray = @[@"5ccf7f0a0a7a"];
    
    intent.icon = [[INObject alloc]initWithIdentifier:intent.sceneNo displayString:@"displayString"];
    [intent setImage:[INImage imageNamed:@"scene_icon_11"] forParameterNamed:@"icon"];
    
    intent.suggestedInvocationPhrase = scene.sceneName;
    
    INShortcut *shortCuts = [[INShortcut alloc] initWithIntent:intent];
    
    INUIAddVoiceShortcutViewController *vc = [[INUIAddVoiceShortcutViewController alloc] initWithShortcut:shortCuts];
    
    vc.delegate = self;
    
    [self presentViewController:vc animated:YES completion:^{
        NSLog(@"aaa");
        
    }];
}


///MARK:打开Siri编辑短语页面
- (void)editSiriShortcut:(INVoiceShortcut *)voiceShortcut  API_AVAILABLE(ios(12.0)){
    //if (@available(iOS 12.0, *)) {
    
    INUIEditVoiceShortcutViewController *editVC = [[INUIEditVoiceShortcutViewController alloc] initWithVoiceShortcut:voiceShortcut];
    editVC.delegate = self;
    //@weakify(self);
    [self presentViewController:editVC animated:YES completion:^{
        //@strongify(self);
        
    }];
    //}
}



#pragma mark- INUIAddVoiceShortcutViewControllerDelegate

/*!
 @abstract Called after the user finishes the setup flow for the voice shortcut, with either the successfully-added voice shortcut, or an error.
 @discussion Your implementation of this method should dismiss the view controller.
 */
- (void)addVoiceShortcutViewController:(INUIAddVoiceShortcutViewController *)controller didFinishWithVoiceShortcut:(nullable INVoiceShortcut *)voiceShortcut error:(nullable NSError *)error API_AVAILABLE(ios(12.0)){
    
    NSLog(@"voiceShortcut == %@",NSStringFromClass([voiceShortcut.shortcut.intent class]));
    
    if(!error){
        [self donateInteration:voiceShortcut];
    }
    [controller dismissViewControllerAnimated:YES completion:^{
        
    }];
}

/*!
 @abstract Called if the user cancels the setup flow; the voice shortcut was not added.
 @discussion Your implementation of this method should dismiss the view controller.
 */
- (void)addVoiceShortcutViewControllerDidCancel:(INUIAddVoiceShortcutViewController *)controller API_AVAILABLE(ios(12.0)){
    [controller dismissViewControllerAnimated:YES completion:^{
        
    }];
}


#pragma mark- INUIEditVoiceShortcutViewControllerDelegate

/*!
 @abstract Called if the user updates the voice shortcut, with either the successfully-updated voice shortcut, or an error.
 @discussion Your implementation of this method should dismiss the view controller.
 */
- (void)editVoiceShortcutViewController:(INUIEditVoiceShortcutViewController *)controller didUpdateVoiceShortcut:(nullable INVoiceShortcut *)voiceShortcut error:(nullable NSError *)error API_AVAILABLE(ios(12.0)){
    if(!error){
        [self donateInteration:voiceShortcut];
    }
    [controller dismissViewControllerAnimated:YES completion:^{
        
    }];
}

/*!
 @abstract Called if the user deletes the voice shortcut.
 @discussion Your implementation of this method should dismiss the view controller.
 */
- (void)editVoiceShortcutViewController:(INUIEditVoiceShortcutViewController *)controller didDeleteVoiceShortcutWithIdentifier:(NSUUID *)deletedVoiceShortcutIdentifier API_AVAILABLE(ios(12.0)){
    [controller dismissViewControllerAnimated:YES completion:^{
        
    }];
}

/*!
 @abstract Called if the user cancelled; no changes were made to the voice shortcut.
 @discussion Your implementation of this method should dismiss the view controller.
 */
- (void)editVoiceShortcutViewControllerDidCancel:(INUIEditVoiceShortcutViewController *)controller API_AVAILABLE(ios(12.0)){
    [controller dismissViewControllerAnimated:YES completion:^{
        
    }];
}


@end
